<?php
/**
 * Description of InformacaoDAO
 *
 * @author Magno
 */
class InformacaoDAO {

    private $conexao;
    private $statusDAO;
    private $clienteDAO;

    public function __construct() {
        $this->conexao = Conexao::getConexao();
        $this->statusDAO = new StatusDAO();
        $this->clienteDAO = new ClienteDAO();
    }

    public function salvar($informacao) {

        try {

            if ($informacao->getInformacaoID() != null && $informacao->getInformacaoID() > 0) {

                $statment = $this->conexao->prepare("UPDATE informacao SET
                        descricao = :descricao,
                        tipo = :tipo,
                        usuarioID = :usuarioID,
                        statusID = :statusID
                        WHERE informacaoID = :informacaoID");
            } else {

                $statment = $this->conexao->prepare("INSERT INTO informacao(informacaoID,descricao,tipo,usuarioID,statusID)
                    VALUES (:informacaoID,:descricao,:tipo,:usuarioID:,statusID);");
            }

            if ($statment->execute($informacao->toBD())) {

                if ($informacao->getInformacaoID() == null || $informacao->getInformacaoID() <= 0)
                    $informacao->setInformacaoID($this->conexao->lastInsertId());
                return $informacao;
            }else {
                $erros = $statment->errorInfo();
                throw new Exception("Erro InformacaoDAO: " . $erros[2]);
            }
        } catch (PDOException $err) {
            throw new Exception("Erro InformacaoDAO:" . $err->getMessage());
        }
    }

    public function excluir($informacaoID) {

        try {

            $statment = $this->conexao->prepare("DELETE FROM informacao WHERE informacaoID = :informacaoID");
            $statment->bindParam(':informacaoID', $informacaoID);

            if (!$statment->execute()) {
                $erros = $statment->errorInfo();
                throw new Exception("Erro InformacaoDAO: " . $erros[2]);
            }
        } catch (PDOException $err) {
            throw new Exception("Erro InformacaoDAO:" . $err->getMessage());
        }
    }

    public function listar($pagina = 0, $tipo = -1, $usuarioID = -1, $statusID = -1, $carregarDependencias = false) {
        try {

            $limite = Util::getPaginacao($pagina);
            $where = $this->getWhere($tipo, $usuarioID, $statusID);
            $informacaos = array();
            $statment = $this->conexao->query("SELECT * FROM informacao u $where $limite");

            foreach ($statment->fetchAll(PDO::FETCH_SERIALIZE) as $row){
                $informacao = new InformacaoBean($row['informacaoID'], $row['descricao'], $row['data'], $row['tipo']);
                $informacao->setStatus($this->statusDAO->buscarPorID($row['statusID']));

                if($carregarDependencias)
                    $informacao->setCliente ($this->clienteDAO->buscarPorID($row['usuarioID']));

                $informacaos[] =  $informacao;
            }

            return $informacaos;
        } catch (PDOException $err) {
            throw new Exception("Erro InformacaoDAO:" . $err->getMessage());
        }
    }

    public function listarPorOrdem($pagina = 0, $tamanho = 0, $ordem = "informacaoID", $direcao = "ASC", $carregarDependencias = false) {
        try {

            $limite = Util::getPaginacao($pagina,$tamanho);
            $informacaos = array();
            $statment = $this->conexao->query("SELECT * FROM informacao u ORDER BY u.$ordem $direcao $limite");

            foreach ($statment->fetchAll(PDO::FETCH_SERIALIZE) as $row){
                $informacao = new InformacaoBean($row['informacaoID'], $row['descricao'], $row['data'], $row['tipo']);
                $informacao->setStatus($this->statusDAO->buscarPorID($row['statusID']));

                if($carregarDependencias)
                    $informacao->setCliente ($this->clienteDAO->buscarPorID($row['usuarioID']));
                
                $informacaos[] = $informacao;
            }

            return $informacaos;
        } catch (PDOException $err) {
            throw new Exception("Erro InformacaoDAO:" . $err->getMessage());
        }
    }

    public function buscarPorID($informacaoID, $carregarDependencias = false) {
        try {

            $row = $this->conexao->query("SELECT * FROM informacao WHERE informacaoID = $informacaoID ")->fetch(PDO::FETCH_SERIALIZE);

            if (isset ($row['informacaoID']) && $row['informacaoID'] > 0) {
                $informacao = new InformacaoBean($row['informacaoID'], $row['descricao'], $row['data'], $row['tipo']);
                $informacao->setStatus($this->statusDAO->buscarPorID($row['statusID']));

                if($carregarDependencias)
                    $informacao->setCliente ($this->clienteDAO->buscarPorID($row['usuarioID']));
                return $informacao;
            } else {
                return null;
            }
        } catch (PDOException $err) {
            throw new Exception("Erro InformacaoDAO:" . $err->getMessage());
        }
    }

    private function getWhere($tipo = -1, $usuarioID = -1, $statusID = -1) {

        $where = " WHERE 1 = 1 ";

        if (isset($usuarioID) && $usuarioID > 0) {
            $where = $where . " AND u.usuario = $usuarioID ";
        }

        if (isset($tipo) && $tipo > 0) {
            $where = $where . " AND u.tipo = $tipo ";
        }

        if (isset($statusID) && $statusID > 0) {
            $where = $where . " AND u.statusID = $statusID ";
        }

        return $where;
    }

    public function total($tipo = -1, $usuarioID = -1, $statusID = -1) {
        try {

            $where = $this->getWhere($tipo, $usuarioID, $statusID);
            $total = $this->conexao->query("SELECT COUNT(u.informacaoID) total FROM informacao u $where")->fetch(PDO::FETCH_COLUMN);
            return $total;
        } catch (PDOException $err) {
            throw new Exception("Erro InformacaoDAO:" . $err->getMessage());
        }
    }

}
?>